//
//  DoTaskView.swift
//  LeGou
//
//  Created by lxm on 2022/8/4.
//

import UIKit

class RedTaskView: XMBaseView {

    //MARK: --------------------属性--------------------
    var info = ZMUtil.fastNilBehavior(type: HomeModels.SignInInfo.self)

    var disposeBag = DisposeBag()
    
    var payCount = 0
    
    var invite = 0
    //MARK: --------------------UI控件--------------------
    
    @IBOutlet weak var contentView: UIView!
    
    @IBOutlet weak var inviteOne: UILabel!
    
    @IBOutlet weak var inviteTwo: UILabel!
    
    @IBOutlet weak var inviteBtn: UIButton!
    
    @IBOutlet weak var taskOne: UILabel!
    
    @IBOutlet weak var taskTwo: UILabel!
    
    @IBOutlet weak var taskBtn: UIButton!
    
    @IBOutlet weak var taskLabel: UILabel!
    
    
    let loading = XMLoadingView()
    
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
        
        getInfo()
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        ZMUtil.clipCircle(view: contentView, Corner: [.topLeft, .topRight], radius: 12)
    }
    
    //MARK: --------------------UI--------------------
    func setUI() {
        self.taskLabel.text = "每月邀请5人，每人完成1笔消费/任务或者每月完成9笔即可参与抢红包"
        self.inviteOne.text = "邀请5位注册用户(\(invite)/5)"
        if invite >= 5 {
            self.inviteBtn.setTitle("已完成", for: .normal)
            self.inviteBtn.setTitleColor("#DE3F26".zm.hex(), for: .normal)
            self.inviteBtn.backgroundColor = UIColor.zm.color(hexString: Style.mainColorStr, 0.1)
            self.inviteBtn.isEnabled = false
            self.inviteTwo.text = "本月完成"
        } else {
            self.inviteBtn.setTitle("去完成", for: .normal)
            self.inviteBtn.setTitleColor("#ffffff".zm.hex(), for: .normal)
            self.inviteBtn.backgroundColor = "#DE3F26".zm.hex()
            self.inviteTwo.text = "本月完成邀请5位好友注册"
        }
        
        self.taskOne.text = "完成9笔消费/悬赏任务(\(payCount)/9)"
        if payCount >= 9{
            self.taskBtn.setTitle("已完成", for: .normal)
            self.taskBtn.setTitleColor("#DE3F26".zm.hex(), for: .normal)
            self.taskBtn.backgroundColor = UIColor.zm.color(hexString: Style.mainColorStr, 0.1)
            self.taskTwo.text = "本月完成"
            self.taskBtn.isEnabled = false
        } else {
            self.taskBtn.setTitle("去完成", for: .normal)
            self.taskBtn.setTitleColor("#ffffff".zm.hex(), for: .normal)
            self.taskBtn.backgroundColor = "#DE3F26".zm.hex()
            self.taskTwo.text = "完成9笔消费/悬赏任务"
        }
    }
    
    func addConstraints() {
     
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
//        info.subscribe(onNext:{ [unowned self] resp in
//            guard let resp = resp else {return}
//            self.taskLabel.text = "每月邀请5人，每人完成1笔消费/任务或者每月完成9笔即可参与抢红包"
//            self.inviteOne.text = "邀请\(resp.invite)位注册用户(\(resp.inviteNumber)/\((resp.invite)))"
//            if resp.inviteNumber >= resp.invite {
//                self.inviteBtn.setTitle("已完成", for: .normal)
//                self.inviteBtn.setTitleColor("#DE3F26".zm.hex(), for: .normal)
//                self.inviteBtn.backgroundColor = UIColor.zm.color(hexString: Style.mainColorStr, 0.1)
//                self.inviteBtn.isEnabled = false
//                self.inviteTwo.text = "本月完成"
//            } else {
//                self.inviteBtn.setTitle("去完成", for: .normal)
//                self.inviteBtn.setTitleColor("#ffffff".zm.hex(), for: .normal)
//                self.inviteBtn.backgroundColor = "#DE3F26".zm.hex()
//                self.inviteTwo.text = "完成邀请\(resp.invite)位好友注册"
//            }
//
//
//
//            self.taskOne.text = "完成\(resp.task)笔消费/悬赏任务(\(resp.taskNumber > resp.task ? resp.task : resp.taskNumber)/\(resp.task))"
//            if resp.taskNumber >= resp.task {
//                self.taskBtn.setTitle("已完成", for: .normal)
//                self.taskBtn.setTitleColor("#DE3F26".zm.hex(), for: .normal)
//                self.taskBtn.backgroundColor = UIColor.zm.color(hexString: Style.mainColorStr, 0.1)
//                self.taskTwo.text = "本月完成"
//                self.taskBtn.isEnabled = false
//            } else {
//                self.taskBtn.setTitle("去完成", for: .normal)
//                self.taskBtn.setTitleColor("#ffffff".zm.hex(), for: .normal)
//                self.taskBtn.backgroundColor = "#DE3F26".zm.hex()
//                self.taskTwo.text = "完成\(resp.task)笔消费/悬赏任务"
//            }
//
//        }).disposed(by: disposeBag)
    }
    
    func getInfo() {
//        loading.showInView(fatherView: contentView)
//        HomeAPI.signInInfo.rxRequest(type: HomeModels.SignInInfo.self).subscribe(onNext:{ [unowned self] resp in
//            self.loading.hideView(anmation: true)
//            self.info.accept(resp)
//        }, onError: { e in
//            self.loading.showError(e.localizedDescription)
//        }).disposed(by: disposeBag)
    }
    
    @IBAction func onClose(_ sender: Any) {
        removeFromSuperview()
    }
    
    @IBAction func onInvite(_ sender: Any) {
        removeFromSuperview()
        router.push("xm://main/Mine/invite")
    }
    
    @IBAction func onTask(_ sender: Any) {
        removeFromSuperview()
//        if LoginService.shared.checkLogin() == false {return}
//        let vc = GameWeb()
//        let url = getgameUrl(userId: ZMAppShareData.share.safeUserId)
//        UIPasteboard.general.string = url
//        vc.url = url
//        AppHelper.currentNav()?.pushViewController(vc, animated: true)
        CATransaction.setCompletionBlock {
            guard let tab = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController else {return}
            tab.selectedIndex = 0
        }
        CATransaction.begin()
        AppHelper.currentNav()?.popToRootViewController(animated: true)
        CATransaction.commit()
    }
    
    
    @IBAction func onSign(_ sender: Any) {
        removeFromSuperview()
        guard let tab = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController else {return}
        tab.selectedIndex = 3
//        let vc = SignInVC()
//        AppHelper.currentNav()?.pushViewController(vc, animated: true)

    }
    //MARK: --------------------delegate--------------------

}
